﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Utilities;
using ViCode_LeVi.Data;
using Xceed.Wpf.DataGrid;
using CommandBinding;
using ViCode_LeVi.Configs;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTaskItemGroup.xaml
    /// </summary>
    public partial class VMTaskItemGroup : UserControl
    {
        public VMTaskItemGroup()
        {
            InitializeComponent();
            new PropertyChangeNotifier(this, FrameworkElement.DataContextProperty, (s, e) =>
            {
                DisplayData();
            });
            this.Loaded += new RoutedEventHandler(VMTaskItemGroup_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(VMTaskItemGroup_SizeChanged);
        }

        void VMTaskItemGroup_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Setup_GridViewColumn_Define();
        }

        void VMTaskItemGroup_Loaded(object sender, RoutedEventArgs e)
        {
            SetupDisplay();
            Setup_GridViewColumn_Define();
            SetupBindingCommand();
        }
        private void SetupDisplay()
        {
            Step_TaskItemGroup data = this.DataContext as Step_TaskItemGroup;
            if (data != null)
            {
            }
        }
        void Setup_GridViewColumn_Define()
        {
            GridView gv = ListView_TaskItems.View as GridView;
            if (gv != null && gv.Columns.Count > 0)
            {
                var sumW = gv.Columns.Where(c => c != GridViewColumn_Define).Select(c => c.ActualWidth).Sum();
                var w = ListView_TaskItems.ActualWidth - sumW - 20;
                if (w > 0)
                    GridViewColumn_Define.Width = w;
            }
        }
        private void SetupBindingCommand()
        {
            this.AssignCondition(Button_AddNew, (s, e) =>
                {
                    Step_TaskItemGroup data = this.DataContext as Step_TaskItemGroup;
                    if (data != null)
                    {
                        //Gen ID                        
                        string seperator = "-";
                        string sCode = data.Code + seperator;
                        var maxId = data.Tasks_Source.Where(c => c.ID.StartsWith(sCode)).Max(c => c.ID);
                        int nOldIDNo = 0;
                        if (!string.IsNullOrEmpty(maxId))
                        {
                            string sNumber = maxId.Replace(sCode, string.Empty);
                            int.TryParse(sNumber, out nOldIDNo);
                        }
                        int nNewId = nOldIDNo + 1;
                        string sNewId = string.Format("{0}{1}", sCode, nNewId);
                        data.Tasks_Source.Add(new Step_TaskItem()
                        {
                            ID = sNewId,
                            Active = VMConfig.Intance.VMConfig_Step_TaskItem_Default_Item.Active,
                            Define = VMConfig.Intance.VMConfig_Step_TaskItem_Default_Item.Define,
                            Name = VMConfig.Intance.VMConfig_Step_TaskItem_Default_Item.Name,
                            Weight = VMConfig.Intance.VMConfig_Step_TaskItem_Default_Item.Weight
                        });
                    }
                }, (s, e) =>
                    {
                        e.OnCanExecute_SyncButtonVisibility(true, true);
                    });
            this.AssignCondition(Button_Remove, (s, e) =>
            {
                if (MessageBox.Show("Do you want to delete this task?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    Step_TaskItemGroup data = this.DataContext as Step_TaskItemGroup;
                    if (data != null)
                    {
                        Step_TaskItem deleteItem = ListView_TaskItems.SelectedItem as Step_TaskItem;
                        if (deleteItem != null)
                        {
                            data.Tasks_Source.Remove(deleteItem);
                        }
                    }
                }
            }, (s, e) =>
            {
                e.OnCanExecute_SyncButtonVisibility(true, ListView_TaskItems.SelectedItem != null);

            });
        }

        private void DisplayData()
        {
            SetupDisplay();
        }
    }
}
